/*
 *  Copyright 2011.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.pc.ui.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.text.MessageFormat;
import java.util.Properties;

/**
 *
 */
public class JProperties extends Properties {

	private static final long serialVersionUID = 7726320230533930608L;

	public JProperties(Properties defaults) {
        putAll(defaults);
    }

    public JProperties() {
    }

    public double getDouble(String key){
        return getDouble(key, 0);
    }

    public double getDouble(String key, double def){
        String val = super.getProperty(key, String.valueOf(def));
        double ret = 0;
        try {
            ret = Double.parseDouble(val);
        } catch (NumberFormatException numberFormatException) {
            return def;
        }
        return ret;
    }

    public void setDouble(String key, double val){
        super.setProperty(key, String.valueOf(val));
    }

    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int def){
        String val = super.getProperty(key, String.valueOf(def));
        int ret = 0;
        try {
            ret = Integer.parseInt(val);
        } catch (NumberFormatException numberFormatException) {
            return def;
        }
        return ret;
    }

    public void setInt(String key, int val){
        super.setProperty(key, String.valueOf(val));
    }
    
    public boolean getBoolean(String key){
        return getBoolean(key, false);
    }
    
    public boolean getBoolean(String key, boolean defval){
        String property = super.getProperty(key, String.valueOf(defval));
        return Boolean.parseBoolean(property);
    }

    public void setBoolean(String key, boolean val){
        super.setProperty(key, String.valueOf(val));
    }

    public Color getColor(String key){
        return getColor(key, null);
    }

    public Color getColor(String key, Color def){
        Color ret = null;
        String val = super.getProperty(key);
        try {
            ret = strToColor(val);
        } catch (IllegalArgumentException illegalArgumentException) {
            return def;
        }
        return ret;
    }

    public void setColor(String key, Color val){
        super.setProperty(key, colorToStr(val));
    }

    public Font getFont(String key) {
        return getFont(key, null);
    }

    public Font getFont(String key, Font def) {
        String val = super.getProperty(key, fontToStr(def));
        Font ret = null;
        try {
            ret = strToFont(val);
        } catch (IllegalArgumentException illegalArgumentException) {
            return def;
        }
        return ret;
    }

    public void setFont(String key, Font val){
        super.setProperty(key, fontToStr(val));
    }

    public Dimension getDimension(String key) {
        return getDimension(key, null);
    }

    public Dimension getDimension(String key, Dimension def) {
        String val = super.getProperty(key, dimensionToStr(def));
        Dimension ret = null;
        try {
            ret = strToDimension(val);
        } catch (IllegalArgumentException illegalArgumentException) {
            return def;
        }
        return ret;
    }

    public void setDimension(String key, Dimension val){
        super.setProperty(key, dimensionToStr(val));
    }

    public Point getPoint(String key) {
        return getPoint(key, null);
    }

    public Point getPoint(String key, Point def) {
        String val = super.getProperty(key, pointToStr(def));
        Point ret = null;
        try {
            ret = strToPoint(val);
        } catch (IllegalArgumentException illegalArgumentException) {
            return def;
        }
        return ret;
    }

    public void setPoint(String key, Point val){
        super.setProperty(key, pointToStr(val));
    }


    public static String colorToStr(Color color){
        if (color == null){
            return null;
        }
        String colString = Integer.toHexString(color.getRGB() & 0xffffff);
        return "#000000".substring(0,7 - colString.length()).concat(colString);
    }

    public static Color strToColor(String str) throws IllegalArgumentException {
        if (str == null || str.length() == 0) {
            throw new IllegalArgumentException("str can be null or empty");
        } else if (str.charAt(0) == '#') {
            return Color.decode(str);
        } else if ("red".equals(str)) {
            return Color.red;
        } else if ("green".equals(str)) {
            return Color.green;
        } else if ("blue".equals(str)) {
            return Color.blue;
        } else if ("yellow".equals(str)) {
            return Color.yellow;
        } else if ("orange".equals(str)) {
            return Color.orange;
        } else if ("white".equals(str)) {
            return Color.white;
        } else if ("lightGray".equals(str)) {
            return Color.lightGray;
        } else if ("gray".equals(str)) {
            return Color.gray;
        } else if ("darkGray".equals(str)) {
            return Color.darkGray;
        } else if ("black".equals(str)) {
            return Color.black;
        } else if ("cyan".equals(str)) {
            return Color.cyan;
        } else if ("magenta".equals(str)) {
            return Color.magenta;
        } else if ("pink".equals(str)) {
            return Color.pink;
        } else {
            throw new IllegalArgumentException(str + " - illegal color name");
        }
    }

    public static String fontToStr(Font font){
        if (font == null){
            return null;
        }
        return font.getName() + "," + font.getStyle() + "," + font.getSize();
    }
    
    public static Font strToFont(String str) throws IllegalArgumentException {
        if (str == null || str.length() == 0) {
            throw new IllegalArgumentException("str can be null or empty");
        }
        String[] arr = str.split(",");
        if (arr.length > 2){
            String name = arr[0].trim();
            int style = Integer.parseInt(arr[1].trim());
            int size = Integer.parseInt(arr[2].trim());
            return new Font(name, style, size);
        } else {
            throw new IllegalArgumentException(str + " is not font");
        }
    }

    public static String dimensionToStr(Dimension dimension){
        if (dimension == null){
            return null;
        }
        return dimension.width + "x" + dimension.height;
    }

    public static Dimension strToDimension(String str) throws IllegalArgumentException {
        if (str == null || str.length() == 0) {
            throw new IllegalArgumentException("str can be null or empty");
        }
        String[] arr = str.split("x");
        if (arr.length > 1){
            int width = Integer.parseInt(arr[0].trim());
            int height = Integer.parseInt(arr[1].trim());
            return new Dimension(width, height);
        } else {
            throw new IllegalArgumentException(str + " is not dimension");
        }
    }

    public static String pointToStr(Point point){
        if (point == null){
            return null;
        }
        return point.x + ";" + point.y;
    }

    public static Point strToPoint(String str) throws IllegalArgumentException {
        if (str == null || str.length() == 0) {
            throw new IllegalArgumentException("str can be null or empty");
        }
        String[] arr = str.split(";");
        if (arr.length > 1){
            int x = Integer.parseInt(arr[0].trim());
            int y = Integer.parseInt(arr[1].trim());
            return new Point(x, y);
        } else {
            throw new IllegalArgumentException(str + " is not point");
        }
    }

    public String getFormatProperty(String key, Object... arguments) {
        return getFormatProperty(key, null, arguments);
    }

    public String getFormatProperty(String key, String def, Object... arguments) {
        String property = super.getProperty(key, def);
        String format = null;
        if (property != null) {
            format = MessageFormat.format(property, arguments);
        }
        return format;
    }

}
